package cn.myfirefly.moming.common.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.common.dao.UserDao;
import cn.myfirefly.moming.common.dao.UserRoleDao;
import cn.myfirefly.moming.common.entity.MMUser;
import cn.myfirefly.moming.common.entity.MMUserRole;
import cn.myfirefly.moming.common.model.RoleDto;
import cn.myfirefly.moming.common.model.UserDto;
import cn.myfirefly.moming.common.model.UserFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.EncryptUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class UserService {
//	private Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private UserRoleDao userRoleDao;
	
	public PageResultDto<MMUser> getList(UserFilterDto dto, boolean isAdmin) {
		
		return userDao.getList(dto, isAdmin);
	}
	
	public PageResultDto<MMUser> getList(UserDto dto) {
		
		return userDao.getList(dto);
	}
	
	public String save(UserDto dto) {
		MMUser user = null;
		// 更新
		if (StrUtil.notBlank(dto.getUserSn())) {
			user = userDao.get(dto.getUserSn());
			if (user == null) {
				return "用户信息不存在，请刷新页面后重试。";
			}
			MMUser tempUser = userDao.getUserByLoginName(dto.getLoginName());
			if (tempUser != null && !tempUser.getUserSn().equals(user.getUserSn())) {
				return "登录账号已存在。";
			}
		} else {
		// 新增
			user = userDao.getUserByLoginName(dto.getLoginName());
			if (user != null) {
				return "登录账号已存在。";
			} 
			user = new MMUser();
			user.setStatus('0');
			user.setChangePass('1');
			user.setCreateBy(dto.getModifyBy());
			user.setCreateDate(dto.getModifyDate());
		}
		BeanUtil.copyBeans(dto, user);
		Integer userSn = userDao.save(user);
		
		// 先删除角色和用户关联数据
		userRoleDao.delByUserSn(userSn);
		// 再新增角色和用户关联数据
		if (StrUtil.notBlank(dto.getRoleSn())) {
			String[] roleSns = dto.getRoleSn().split(",");
			MMUserRole userRole = null;
			for (int i = 0; i < roleSns.length; i++) {
				userRole = new MMUserRole(Integer.valueOf(roleSns[i]), userSn);
				userRoleDao.save(userRole);
			}
		}
		
		return Const.ResultCode.SUCCESS;
	}
	
	public UserDto getUser(Integer userSn) {
		MMUser user = userDao.get(userSn);
		UserDto userDto = BeanUtil.copyBeans(UserDto.class, user);
		// 取得用户角色数据
		List<RoleDto> list = userRoleDao.getRoles(userSn);
		if (list != null) {
			int size = list.size();
			StringBuffer roleSn = new StringBuffer();
			StringBuffer roleName = new StringBuffer();
			RoleDto role = null;
			for (int i = 0; i < size; i++) {
				role = list.get(i);
				if (i+1 >= size) {
					roleSn.append(role.getRoleSn());
					roleName.append(role.getRoleName());
				} else {
					roleSn.append(role.getRoleSn()).append(",");
					roleName.append(role.getRoleName()).append(",");
				}
			}
			userDto.setRoleSn(roleSn.toString());
			userDto.setRoleName(roleName.toString());
		}
		
		return userDto;
	}
	
	public UserDto getUser(String loginName) {
		MMUser user = userDao.getUserByLoginName(loginName);
		return BeanUtil.copyBeans(UserDto.class, user);
	}
	
	public String del(Integer userSn) {
		MMUser user = userDao.get(userSn);
		if (user == null) {
			return "用户信息不存在，请刷新页面后重试。";
		}
		userDao.del(user);
		return Const.ResultCode.SUCCESS;
	}
	
	public String setPasswd(UserDto dto, String passwd) {
		MMUser user = userDao.get(dto.getUserSn());
		if (user == null) {
			return "用户信息不存在，请刷新页面后重试。";
		}
		user.setPassword(EncryptUtil.md5(passwd));
		user.setModifyBy(dto.getModifyBy());
		user.setModifyDate(dto.getModifyDate());
		userDao.save(user);
		return Const.ResultCode.SUCCESS;
	}
	
	public String changeStatus(UserDto dto) {
		MMUser user = userDao.get(dto.getUserSn());
		if (user == null) {
			return "用户信息不存在，请刷新页面后重试。";
		}
		user.setStatus(dto.getStatus());
		user.setModifyBy(dto.getModifyBy());
		user.setModifyDate(dto.getModifyDate());
		userDao.save(user);
		return Const.ResultCode.SUCCESS;
	}
	
	public void updateLastLoginTime(Integer userSn) {
		MMUser user = userDao.get(userSn);
		if (user != null) {
			user.setLastLoginTime(new Date());
			userDao.save(user);
		}
	}
}
